package net.zhengxinyun.performance.service.criteria;

import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.zhengxinyun.performance.entity.CriteriaEntity;
import net.zhengxinyun.performance.filter.CriteriaFilter;
import net.zhengxinyun.performance.mapper.criteria.CriteriaEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * Date: 2020/3/16 14:42
 * 考评规则
 */
@Service
public class CriteriaServiceImpl implements CriteriaService {
    @Autowired
    private CriteriaEntityMapper mapper;

    /**
     * 考评机制的增删改查；
     * @param filter
     * @return
     */
    @Override
    public HashMap<String, Object> criteriaManage(CriteriaFilter filter) {
        String optionType = filter.getOptionType();
        HashMap<String, Object> map = new HashMap<>();
        if (StringUtils.isBlank(optionType)) {
            map.put("code", 500);
            map.put("msg", "参数错误，optionType不能为空");
            return map;
        }
        switch (optionType) {
            case "add":
                Long evalId = filter.getEvalId();

                if (checkIsExits(evalId)==1) {
                    //存在我无法在此添加
                    map.put("code", 500);
                    map.put("msg", "该类型已经设置考评规则，无法重复添加");
                    return map;
                }
                CriteriaEntity saveRecord = setValue(filter);
                saveRecord.setEvalId(evalId);
                mapper.insert(saveRecord);
                map.put("code", 200);
                return map;
            case "update":
                if (ObjectUtils.isBlank(filter.getId())) {
                    map.put("code", 500);
                    map.put("msg", "参数错误，id不能为空");
                    return map;
                }
                CriteriaEntity record = null;
                record = setValue(filter);
                record.setId(filter.getId());
                mapper.updateByPrimaryKeySelective(record);
                map.put("code", 200);
                return map;
            case "del":
                break;
            case "query":
                CriteriaFilter querys = new CriteriaFilter();
               querys.setCriteriaType(filter.getCriteriaType());
                if(ObjectUtils.isNotBlank(filter.getId())){
                    querys.setId(filter.getId());
                }
                if(ObjectUtils.isNotBlank(filter.getEvalId()))
                {
                    querys.setEvalId(filter.getEvalId());
                }
                List<CriteriaEntity> lists = mapper.selectListCriteria(querys);
                if (ObjectUtils.isBlank(lists)) {
                    map.put("code", 500);
                    map.put("msg", "查无数据");
                    return map;
                }

                map.put("code", 200);
                map.put("data", lists);
                return map;

        }


        return null;
    }

    /**
     * 先检查是否已经存在；
     */
    public int checkIsExits(Long evalId){

        CriteriaFilter filter = new CriteriaFilter();
        filter.setEvalId(evalId);
        List<CriteriaEntity> list = selectListCriteria(filter);
        if (ObjectUtils.isBlank(list)) {
            return  0;
        }else {
            return 1;
        }

    }


    /**
     * 设置实体类的值
     *
     * @param filter
     * @return
     */
    CriteriaEntity setValue(CriteriaFilter filter) {
        CriteriaEntity entity = new CriteriaEntity();
       // entity.setCriteriaName(filter.getCriteriaName());
        entity.setCriteriaType(filter.getCriteriaType());
        entity.setCriteriaCode(filter.getCriteriaCode());
       // entity.setWeight(filter.getWeight());
        if (filter.getCriteriaType() == 1) {
            entity.setCriteriaValue(filter.getCriteriaValue());
        }
        if (2 == filter.getCriteriaType()) {
            entity.setGoodStandard(filter.getGoodStandard());
            entity.setGeneralStandard(filter.getGeneralStandard());
            entity.setBadStandard(filter.getBadStandard());
            entity.setGoodValue(filter.getGoodValue());
            entity.setGeneralValue(filter.getGeneralValue());
            entity.setBadValue(filter.getBadValue());
        }

        return entity;
    }

    @Override
    public List<CriteriaEntity> selectListCriteria(CriteriaFilter filter) {
        return mapper.selectListCriteria(filter);
    }


}
